package io.github.ali.commons.exception;

import io.github.ali.commons.code.AbstractExceptionCode;
import io.github.ali.commons.code.ExceptionCode;

/**
 * @author ALi
 * @version 1.0
 * @date 2021-10-15 14:22
 * @description
 */
public class BizRuntimeException extends RuntimeException implements ExpressibleException{
	private static final long serialVersionUID = 1L;
	private ExceptionCode exceptionCode;

	public BizRuntimeException() {
		super(AbstractExceptionCode.getDefaultExceptionCode().getRespCode().getMessage());
		this.exceptionCode = AbstractExceptionCode.getDefaultExceptionCode();
	}

	public BizRuntimeException(Throwable e) {
		super(e);
		this.exceptionCode = AbstractExceptionCode.getDefaultExceptionCode();
	}

	public BizRuntimeException(String message) {
		super(message);
		this.exceptionCode = AbstractExceptionCode.getDefaultExceptionCode();
	}

	public BizRuntimeException(Throwable e, String message) {
		super(message, e);
		this.exceptionCode = AbstractExceptionCode.getDefaultExceptionCode();
	}

	public BizRuntimeException(ExceptionCode exceptionCode) {
		super(exceptionCode.getRespCode().getMessage());
		this.exceptionCode = exceptionCode;
	}

	public BizRuntimeException(ExceptionCode exceptionCode, Throwable ex) {
		super(ex);
		this.exceptionCode = exceptionCode;
	}

	public BizRuntimeException(ExceptionCode exceptionCode, String message) {
		super(message);
		this.exceptionCode = exceptionCode;
	}

	public BizRuntimeException(ExceptionCode exceptionCode, Throwable ex, String message) {
		super(message, ex);
		this.exceptionCode = exceptionCode;
	}

	@Override
	public ExceptionCode getExceptionCode() {
		return exceptionCode;
	}

	@Override
	public String toString() {
		if (exceptionCode == null) {
			return super.toString();
		}
		return MessageBuilderManager.getMessageBuilder(this).buildMessage(this, getMessage());
	}
}
